//package com.lucifer.cloud.boot.blog.ai.model.classification;
//
//import lombok.SneakyThrows;
//import org.datavec.api.split.FileSplit;
//import org.datavec.image.loader.NativeImageLoader;
//import org.datavec.image.recordreader.ImageRecordReader;
//import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator;
//import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
//import org.deeplearning4j.util.ModelSerializer;
//import org.nd4j.linalg.api.ndarray.INDArray;
//import org.nd4j.linalg.dataset.DataSet;
//import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
//import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler;
//
//import java.io.File;
//
///**
// * @author lucifer
// * @date 2024/11/28 13:52
// */
//public class PredictImage {
//    public static void main(String[] args) throws Exception {
//        // 加载模型
//        MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(Constant.writeModelPath);
//
//        // 加载待预测的图像
//        INDArray image = loadImage("D:\\data\\tesseract\\cat.1.jpg");
//
//        // 获取预测结果
//        int[] predict = model.predict(image);
//        System.out.println("Predicted class: " + predict[0]);
//    }
//
//    @SneakyThrows
//    private static INDArray loadImage(String path) {
//        FileSplit fileSplit = new FileSplit(new File(path), NativeImageLoader.ALLOWED_FORMATS);
//        ImageRecordReader recordReader = new ImageRecordReader(Constant.height, Constant.width, Constant.channels);
//        recordReader.initialize(fileSplit);
//        DataSetIterator dataIter = new RecordReaderDataSetIterator(recordReader, 1);
//        dataIter.setPreProcessor(new ImagePreProcessingScaler(0, 1));
//        DataSet next = dataIter.next();
//        INDArray features = next.getFeatures();
//        return features;
//    }
//
//    @SneakyThrows
//    public static INDArray loadImage(File file) {
//        FileSplit fileSplit = new FileSplit(file, NativeImageLoader.ALLOWED_FORMATS);
//        ImageRecordReader recordReader = new ImageRecordReader(Constant.height, Constant.width, Constant.channels);
//        recordReader.initialize(fileSplit);
//        DataSetIterator dataIter = new RecordReaderDataSetIterator(recordReader, 1);
//        dataIter.setPreProcessor(new ImagePreProcessingScaler(0, 1));
//        DataSet next = dataIter.next();
//        INDArray features = next.getFeatures();
//        return features;
//    }
//}
